//
//  PageContentCollectionViewCell.swift
//  PageNavigator
//
//  Created by 张奇 on 2025/8/13.
//

import UIKit

class PageContentCollectionViewCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    private var viewController: UIViewController?
    
    func displayViewController(_ viewController: UIViewController) {
        guard viewController != self.viewController else {
            return
        }
        if let old = self.viewController, let oldView = old.view {
            oldView.removeFromSuperview()
        }
        if let vcView = viewController.view {
            self.contentView.addSubview(vcView)
        }
        self.viewController = viewController
    }
    
    func endDisplayViewController(_ viewController: UIViewController) {
        self.viewController = nil
        viewController.view.removeFromSuperview()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        if let vc = viewController, let vcView = vc.view {
            vcView.frame = self.contentView.bounds
        }
    }
    
//    override func willMove(toWindow newWindow: UIWindow?) {
//        super.willMove(toWindow: newWindow)
//        print("method 被执行：\(#function)")
//    }
//    
//    override func didMoveToWindow() {
//        super.didMoveToWindow()
//        print("method 被执行：\(#function)")
//    }
    
}
